//
// File:	EventManager.cpp
// Author:	Thomas TISSOT-DUPONT
// Login:	<thomas.tissotdupont@free.fr>
//
// Created:	02/05/2010 at 20:11:16
// Purpose:	
//

#include "EventManager.hpp"
#include "Trace.hpp"


//------------------------------------------------------------------
// Update
//------------------------------------------------------------------
void EventManager::Update()
{
	/*
	Trace::debug << "EventManager::Update: Events Count: " << m_eventsList.size() << std::endl;
	EventList::const_iterator it = m_eventsList.begin();
	EventList::const_iterator ite = m_eventsList.end();
	for (; it != ite; ++it)
	{
		Trace::debug << "\tEvent: " << (*it).callerFunctionName << std::endl;
	}
	*/
}

//------------------------------------------------------------------
// Connect
//------------------------------------------------------------------
void EventManager::Connect(void *pTarget, void *pTargetEvent,
							void *pCaller, const std::string &callerFunctionName,
							const EventConnection &eventConnection)
{
	EventInfo eventInfo;
	eventInfo.pTarget				= pTarget;
	eventInfo.pTargetEvent			= pTargetEvent;
	eventInfo.pCaller				= pCaller;
	eventInfo.callerFunctionName	= callerFunctionName;
	eventInfo.eventConnection		= eventConnection;
	m_eventsList.push_back(eventInfo);
}

//------------------------------------------------------------------
// Disconnect
//------------------------------------------------------------------
void EventManager::Disconnect(	void *pTarget, void *pTargetEvent,
								void *pCaller, const std::string &callerFunctionName)
{
	EventList::iterator it = m_eventsList.begin();
	EventList::iterator ite = m_eventsList.end();
	while (it != ite)
	{
		EventInfo &eventInfo = *it;
		if (eventInfo.pTarget == pTarget && eventInfo.pTargetEvent == pTargetEvent &&
			eventInfo.pCaller == pCaller && eventInfo.callerFunctionName == callerFunctionName)
		{
			eventInfo.eventConnection.disconnect();
			it = m_eventsList.erase(it);
			continue;
		}
		++it;
	}
}

//------------------------------------------------------------------
// Disconnect Event
//------------------------------------------------------------------
void EventManager::DisconnectEvent(void *pTargetEvent)
{
	EventList::iterator it = m_eventsList.begin();
	EventList::iterator ite = m_eventsList.end();
	while (it != ite)
	{
		EventInfo &eventInfo = *it;
		if (eventInfo.pTargetEvent == pTargetEvent)
		{
			eventInfo.eventConnection.disconnect();
			it = m_eventsList.erase(it);
			continue;
		}
		++it;
	}
}

//------------------------------------------------------------------
// Disconnect Target
//------------------------------------------------------------------
void EventManager::DisconnectTarget(void *pTarget)
{
	EventList::iterator it = m_eventsList.begin();
	EventList::iterator ite = m_eventsList.end();
	while (it != ite)
	{
		EventInfo &eventInfo = *it;
		if (eventInfo.pTarget == pTarget)
		{
			eventInfo.eventConnection.disconnect();
			it = m_eventsList.erase(it);
			continue;
		}
		++it;
	}
}

//------------------------------------------------------------------
// Disconnect Caller
//------------------------------------------------------------------
void EventManager::DisconnectCaller(void *pCaller)
{
	EventList::iterator it = m_eventsList.begin();
	EventList::iterator ite = m_eventsList.end();
	while (it != ite)
	{
		EventInfo &eventInfo = *it;
		if (eventInfo.pCaller == pCaller)
		{
			eventInfo.eventConnection.disconnect();
			it = m_eventsList.erase(it);
			continue;
		}
		++it;
	}
}

//------------------------------------------------------------------
// Disconnect All
//------------------------------------------------------------------
void EventManager::DisconnectAll()
{
	while (!m_eventsList.empty())
	{
		m_eventsList.front().eventConnection.disconnect();
		m_eventsList.erase(m_eventsList.begin());
	}
}
